library("tidyverse")
library("firatheme")
library("prediction")
library("estimatr")
library("texreg")

# load data
load("~/df_replication.RData")

#Table SI 8
summary(model8 <- lm_robust(click ~ cue*elected + issue*elected + region + party_name, data = df_replication, se_type="HC2"))
texreg(model8, digits = 3, include.ci = FALSE)

# Figure SI 13 -  upper panel
a_elected <- prediction(model8, at = list(elected = 1, cue = "problem_indicators"))
b_elected <- prediction(model8, at = list(elected = 0, cue = "problem_indicators"))
c_elected <- prediction(model8, at = list(elected = 1, cue = "public_opinion"))
d_elected <- prediction(model8, at = list(elected = 0, cue = "public_opinion"))
e_elected <- prediction(model8, at = list(elected = 1, cue = "rival_parties"))
f_elected <- prediction(model8, at = list(elected = 0, cue = "rival_parties"))
g_elected <- prediction(model8, at = list(elected = 1, cue = "the_media"))
h_elected <- prediction(model8, at = list(elected = 0, cue = "the_media"))

figure13.1 <- tibble(
  Treatment = c("Problem Indicators","Problem Indicators","Public Opinion", "Public Opinion", "Party Positions", "Party Positions", "Media Reporting", "Media Reporting"),
  Elected = c("Yes","No","Yes", "No", "Yes", "No", "Yes", "No"),
  avg = c(mean(a_elected$fitted)*100, mean(b_elected$fitted)*100, mean(c_elected$fitted)*100, mean(d_elected$fitted)*100, mean(e_elected$fitted)*100, mean(f_elected$fitted)*100, mean(g_elected$fitted)*100, mean(h_elected$fitted)*100), 
  lower = c(mean(a_elected$fitted)*100 - mean(a_elected$se.fitted)*100, mean(b_elected$fitted)*100 - mean(b_elected$se.fitted)*100, mean(c_elected$fitted)*100 - mean(c_elected$se.fitted)*100, mean(d_elected$fitted)*100 - mean(d_elected$se.fitted)*100, mean(e_elected$fitted)*100 - mean(e_elected$se.fitted)*100, mean(f_elected$fitted)*100 - mean(f_elected$se.fitted)*100, mean(g_elected$fitted)*100 - mean(g_elected$se.fitted)*100, mean(h_elected$fitted)*100 - mean(h_elected$se.fitted)*100),
  upper = c(mean(a_elected$fitted)*100 + mean(a_elected$se.fitted)*100, mean(b_elected$fitted)*100 + mean(b_elected$se.fitted)*100, mean(c_elected$fitted)*100 + mean(c_elected$se.fitted)*100, mean(d_elected$fitted)*100 + mean(d_elected$se.fitted)*100, mean(e_elected$fitted)*100 + mean(e_elected$se.fitted)*100, mean(f_elected$fitted)*100 + mean(f_elected$se.fitted)*100, mean(g_elected$fitted)*100 + mean(g_elected$se.fitted)*100, mean(h_elected$fitted)*100 + mean(h_elected$se.fitted)*100))

ggplot(figure13.1, aes(x= reorder(Treatment,-avg), y=avg, fill=Elected)) +
  geom_bar(stat="identity", position="dodge", color= "black", width=.25) +
  geom_linerange(aes(ymin=lower, ymax=upper), position= position_dodge(.25)) +
  geom_text(aes(y=upper, label=format(round(avg, digits=1), nsmall = 1)), size = 3, position=position_dodge(width=0.7), vjust=-1.75)+
  labs(y="Click Rate (%)", x="", title="", caption = "Note: Error bars represent ± 1 standard error (HC2)", size = 4)+
  scale_fill_manual(values=c("grey80","gray50", "grey80","gray50", "grey30","gray50", "grey80","gray50")) + 
  scale_y_continuous(limits = c(0, 45)) + theme_fira() + ggtitle("a") +
  theme(text = element_text(size=8), legend.position= "bottom", plot.caption = element_text(size = 6, color = "gray40", face = "italic"),
        plot.title = element_text(hjust = 0))

# Figure SI 13 - lower panel
i_elected <- prediction(model8, at = list(elected = 1, issue = "climate"))
j_elected <- prediction(model8, at = list(elected = 0, issue = "climate"))
k_elected <- prediction(model8, at = list(elected = 1, issue = "school"))
l_elected <- prediction(model8, at = list(elected = 0, issue = "school"))

figure13.2 <- tibble(
  Problem = c("Climate Change", "Climate Change", "Public Schools","Public Schools"),
  Elected = c("Yes","No","Yes", "No"),
  avg = c(mean(i_elected$fitted)*100, mean(j_elected$fitted)*100, mean(k_elected$fitted)*100, mean(l_elected$fitted)*100), 
  lower = c(mean(i_elected$fitted)*100 - mean(i_elected$se.fitted)*100, mean(j_elected$fitted)*100 - mean(j_elected$se.fitted)*100, mean(k_elected$fitted)*100 - mean(k_elected$se.fitted)*100, mean(l_elected$fitted)*100 - mean(l_elected$se.fitted)*100),
  upper = c(mean(i_elected$fitted)*100 + mean(i_elected$se.fitted)*100, mean(j_elected$fitted)*100 + mean(j_elected$se.fitted)*100, mean(k_elected$fitted)*100 + mean(k_elected$se.fitted)*100, mean(l_elected$fitted)*100 + mean(l_elected$se.fitted)*100))

ggplot(figure13.2, aes(x= Problem, y=avg, fill=Elected)) +
  geom_bar(stat="identity", position="dodge", color= "black", width=.25) +
  geom_linerange(aes(ymin=lower, ymax=upper), position= position_dodge(.25)) +
  geom_text(aes(y=upper, label=format(round(avg, digits=1), nsmall = 1)), size = 3, position=position_dodge(width=0.7), vjust=-1.75)+
  labs(y="Click Rate (%)", x="", title="", caption = "Note: Error bars represent ± 1 standard error (HC2)", size = 4)+
  scale_fill_manual(values=c("grey80","gray50", "grey80","gray50", "grey30","gray50", "grey80","gray50")) + 
  scale_y_continuous(limits = c(0, 40)) + theme_fira() + ggtitle("b") +
  theme(text = element_text(size=8), legend.position= "bottom", plot.caption = element_text(size = 6, color = "gray40", face = "italic"),
        plot.title = element_text(hjust = 0))



